import { reqGetUserPublicList, reqUserLogin, reqUserRegister, reqUserModify, reqGetUserInfo, reqEditUserInfo, reqUserOrderList } from "@/api";
import { setToken, getToken, removeToken } from "@/utils/token";
const state = {
    userPublicList: [],
    userPublicTotalNums:0,
    orderList: [],
    totalNums: 0,
    // 已登录标识符，用于记录（之后应当用token来替代）
    userInfo: {
        userName: ""
    },
    token: getToken(),
};

const actions = {
    // 获取用户发布的商品的信息
    async getUserPublicList({ commit },params) {
        let result = await reqGetUserPublicList(params);
        if (result.code == 200) {
            commit("GETUSERPUBLICLIST", result.data);
            return "ok";
        }else{
            return Promise.reject(result.msg);
        }
    },
    // 获取用户的订单信息
    async getUserOrderList({ commit }, params) {
        let result = await reqUserOrderList(params)
        if (result.code == 200) {
            commit("GETUSERORDERLIST", result.data);
            return 'ok';
        } else {
            return Promise.reject(result.msg)
        }
    },
    // 用户登录
    async userLogin({ commit }, account) {
        let result = await reqUserLogin(account);
        if (result.code == 200) {
            commit("UPDATAUSER", result.data.user);
            commit("UPDATETOKEN", result.data.token);
            return "ok";
        } else {
            return Promise.reject(result.msg);
        }
    },
    // 用户注册
    async userRegister({ dispatch }, account) {
        let result = await reqUserRegister(account);
        if (result.code == 200) {
            await dispatch("userLogin", account);
            return "ok";
        } else {
            return Promise.reject(result.data.msg);
        }
    },
    // 用户修改账号安全信息
    async userModify({ commit }, account) {
        let result = await reqUserModify(account);
        if (result.code == 200) {
            commit("UPDATETOKEN", result.token)
            return 'ok'
        } else {
            if (result.code == 520) {
                // 若是身份过期，则应该清楚登录令牌等身份信息
                commit("CLEAR")
            }
            return Promise.reject(result.msg)
        }
    },
    // 用户修改个人信息
    async editUserInfo({ commit }, newUserInfo) {
        let result = await reqEditUserInfo(newUserInfo);
        if (result.code == 200) {
            commit("UPDATAUSER", result.data.user)
            return "ok"
        } else {
            return Promise.reject(new Error("修改个人信息失败"))
        }
    },
    // 用户退出登录
    async userLogout({ commit }) {
        /*
        在这里进行服务器请求退出登录......
        */
        commit("CLEAR");
    },
    // 获取用户信息
    async getUserInfo({ commit }) {
        let result = await reqGetUserInfo();
        if (result.code == 200) {
            commit("UPDATAUSER", result.data.user)
        } else {
            commit("CLEAR");
            return Promise.reject("登录信息过期,请重新登录！");
        }
    }
};


const mutations = {
    GETUSERPUBLICLIST(state, {goodList,totalNums}) {
        state.userPublicList = goodList;
        state.userPublicTotalNums = totalNums;
    },
    // 获取用户订单信息
    GETUSERORDERLIST(state, { totalNums, orderList }) {
        state.totalNums = totalNums;
        state.orderList = orderList;
    },
    // 登陆之后获取用户的信息，包括id和token
    UPDATAUSER(state, user) {
        state.userInfo = user;
    },
    // 更新token信息
    UPDATETOKEN(state, token) {
        state.token = token
        setToken(token);
    },
    // 退出登录应该清除用户信息，token，取反标识符
    CLEAR(state) {
        state.userPublicList = [];
        state.userBuyList = [];
        // 已登录标识符，用于记录（之后应当用token来替代）
        state.userInfo = {}
        state.token = "";
        removeToken();
    }
};

const getters = {
    userName(state) {
        return state.userInfo.userName || ""
    }
};

export default {
    namespaced: true,
    actions,
    mutations,
    state,
    getters
}